package com.lris.handle;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.lris.enumerates.MessageType;
import com.lris.pojodata.Message;
import com.lris.rest.Result;
import com.lris.server.ImServer;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j;

import java.util.UUID;

@Slf4j
public class UserHandle {

    public static  void execute(ChannelHandlerContext ctx,  TextWebSocketFrame frame){

        try {
            Message chatMessage = JSON.parseObject(frame.text(), Message.class);
            log.info("收到加好友消息");
            switch (MessageType.math(chatMessage.getType())){
                //type:100 添加好友请求
                //code :100
                case ADD -> {
                    if(chatMessage.getMsgType()==100){
                        Channel channel = ImServer.USERS.get(chatMessage.getToId().toString());
                        if (null==channel){
                          ctx.channel().writeAndFlush(Result.fail("添加好友失败,此侠客已退隐江湖"));
                            return;
                        }
                        else {

                            log.info("收到加好友消息");
                            Message message = new Message();
                            // 客户端无需设置
                            message.setMsgId(UUID.randomUUID().toString());
                            //设置fromid
                            message.setAddFromId(chatMessage.getAddFromId());
                            //客户端无需设置
                            message.setAddTime(DateUtil.date().toLocalDateTime());
                            //添加好友验证信息
                            message.setRemark(chatMessage.getRemark());
                            //被添加的用户id
                            message.setToId(chatMessage.getToId());
                            //添加这昵称
                            message.setAddNickname(chatMessage.getNickname());
                            //添加者 头像
                            message.setHeadImg(chatMessage.getHeadImg());
                            //客户端无需设置
                            message.setMsgType(100);
                            //添加者手机号
                            message.setPhone(chatMessage.getPhone());
                            channel.writeAndFlush(Result.success(message));
                            log.info("发送好友请求已发送");
                        }
                    }
                }
                case ADD_OK -> {
                    //type:101 同意添加好友请求
                    if(chatMessage.getMsgType()==101){
                        Channel channel = ImServer.USERS.get(chatMessage.getToId().toString());
                        if (null==channel){
                            ctx.channel().writeAndFlush(Result.fail("添加好友失败,此侠客已退隐江湖"));
                            return;
                        }
                        else {
                            Message message = new Message();
                            message.setMsgId(UUID.randomUUID().toString());
                            message.setAddFromId(chatMessage.getAddFromId());
                            message.setAddTime(DateUtil.date().toLocalDateTime());
                            message.setRemark(chatMessage.getRemark());
                            message.setToId(chatMessage.getToId());
                            message.setAddNickname(chatMessage.getNickname());
                            message.setHeadImg(chatMessage.getHeadImg());
                            message.setMsgType(101);
                            message.setPhone(chatMessage.getPhone());
                            channel.writeAndFlush(Result.success(message));
                            log.info("同意已发送");
                        }
                    }
                }
                //拒绝添加好友请求
                case ON_ADD -> {
                    //type:-100 同意添加好友请求
                    if(chatMessage.getMsgType()==-100) {
                        Channel channel = ImServer.USERS.get(chatMessage.getToId().toString());
                        if (null == channel) {
                            ctx.channel().writeAndFlush(Result.fail("添加好友失败,此侠客已退隐江湖"));
                            return;
                        }else {
                            Message message = new Message();
                            message.setMsgId(UUID.randomUUID().toString());
                            message.setAddFromId(chatMessage.getAddFromId());
                            message.setAddTime(DateUtil.date().toLocalDateTime());
                            message.setRemark(chatMessage.getRemark());
                            message.setToId(chatMessage.getToId());
                            message.setAddNickname(chatMessage.getNickname());
                            message.setHeadImg(chatMessage.getHeadImg());
                            message.setMsgType(-100);
                            message.setPhone(chatMessage.getPhone());
                            channel.writeAndFlush(Result.success(message));
                            log.info("已拒绝的消息已发送给拒绝者");
                        }
                         }

                }

                default -> {
                    log.info("添加失败");
                    ctx.channel().writeAndFlush(Result.fail("添加好友失败"));

                }

            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
